﻿using SIAKAD.Helper;
using SIAKADCORE.Controllers;
using SIAKADCORE.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SIAKAD.Dosens
{
    public partial class ViewPertanyaan : System.Web.UI.Page
    {
        private DataSet dsPertanyaan;

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckSession();
            if (!Page.IsCallback)
            {
                this.InitTabelPertanyaan();
            }
        }

        private void CheckSession()
        {
            if (Session["siakad"] != null && Session["siakad"] is SIAKADCORE.Models.Login)
            {
                if ((Session["siakad"] as SIAKADCORE.Models.Login).UserType != SIAKADCORE.Models.LoginType.Dosen)
                {
                    Response.Redirect(SiteMapHelper.Check((Session["siakad"] as SIAKADCORE.Models.Login)), false);
                    Context.ApplicationInstance.CompleteRequest();
                }
                else
                {
                    this.litName.Text = DosenController.GetName((Session["siakad"] as SIAKADCORE.Models.Login).IdLogin);
                    CheckRole();
                }
            }
            else
            {
                Response.Redirect("/Login.aspx");
            }
        }

        private void CheckRole()
        {
            string username = (Session["siakad"] as SIAKADCORE.Models.Login).Username;
            string idRole = UserController.GetRole(username);

            if (idRole != "RL005")
            {
                Response.Redirect(SiteMapHelper.Check((Session["siakad"] as SIAKADCORE.Models.Login)), false);
                Context.ApplicationInstance.CompleteRequest();
            }
        }

        protected void lnkLogout_Click(object sender, EventArgs e)
        {
            Session.Clear();
            CheckSession();
        }

        #region Helpers
        private void InitTabelPertanyaan()
        {
            bool isSuccess = PertanyaanController.GetTabel(out this.dsPertanyaan);
            if (isSuccess)
            {
                this.gvPertanyaan.DataSource = this.dsPertanyaan.Tables[0];
                this.gvPertanyaan.DataBind();
            }
        }

        protected void SetHiddenField(int index, ref HiddenField hiddenField)
        {
            string code = gvPertanyaan.DataKeys[index].Value.ToString();
            hiddenField.Value = code;
        }

        private void DetailCommand(int index)
        {
            this.SetHiddenField(index, ref this.hfDetailID);
            var recent = this.GetRow(this.hfDetailID.Value);

            this.txtDetailPertanyaan.Text = recent.IsiPertanyaan;
            this.txtDetailPilihanJawaban1.Text = recent.PilihanJawaban1;
            this.txtDetailPilihanJawaban2.Text = recent.PilihanJawaban2;
            this.txtDetailPilihanJawaban3.Text = recent.PilihanJawaban3;
            this.txtDetailPilihanJawaban4.Text = recent.PilihanJawaban4;
            this.txtDetailBobotPillihanJawaban1.Text = Convert.ToString(recent.BobotPillihanJawaban1);
            this.txtDetailBobotPillihanJawaban2.Text = Convert.ToString(recent.BobotPillihanJawaban2);
            this.txtDetailBobotPillihanJawaban3.Text = Convert.ToString(recent.BobotPillihanJawaban3);
            this.txtDetailBobotPillihanJawaban4.Text = Convert.ToString(recent.BobotPillihanJawaban4);


            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowDetailModalScript", PertanyaanScript.ShowDetail(), false);
        }

        private void EditCommand(int index)
        {
            this.SetHiddenField(index, ref this.hfUpdateID);
            var recent = this.GetRow(this.hfUpdateID.Value);

            this.txtEditPertanyaan.Text = recent.IsiPertanyaan;
            this.txtEditPilihanJawaban1.Text = recent.PilihanJawaban1;
            this.txtEditPilihanJawaban2.Text = recent.PilihanJawaban2;
            this.txtEditPilihanJawaban3.Text = recent.PilihanJawaban3;
            this.txtEditPilihanJawaban4.Text = recent.PilihanJawaban4;
            this.ddlEditBobotPillihanJawaban1.SelectedValue = recent.BobotPillihanJawaban1.ToString();
            this.ddlEditBobotPillihanJawaban2.SelectedValue = recent.BobotPillihanJawaban2.ToString();
            this.ddlEditBobotPillihanJawaban3.SelectedValue = recent.BobotPillihanJawaban3.ToString();
            this.ddlEditBobotPillihanJawaban4.SelectedValue = recent.BobotPillihanJawaban4.ToString();
            //this.ddlEditBobotPillihanJawaban1.Items.FindByValue(recent.BobotPillihanJawaban1.ToString()).Selected = true;
            //this.ddlEditBobotPillihanJawaban2.Items.FindByValue(recent.BobotPillihanJawaban2.ToString()).Selected = true;
            //this.ddlEditBobotPillihanJawaban3.Items.FindByValue(recent.BobotPillihanJawaban3.ToString()).Selected = true;
            //this.ddlEditBobotPillihanJawaban4.Items.FindByValue(recent.BobotPillihanJawaban4.ToString()).Selected = true;

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowEditModalScript", PertanyaanScript.ShowEdit(), false);
        }

        private void DeleteCommand(int index)
        {
            this.SetHiddenField(index, ref this.hfDeleteID);

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowDeleteModalScript", PertanyaanScript.ShowDelete(), false);
        }

        protected Pertanyaan GetRow(string idPertanyaan)
        {
            var recent = this.dsPertanyaan.Tables[0].Rows.OfType<DataRow>().FirstOrDefault(row => row[0].ToString() == idPertanyaan);

            return new Pertanyaan
            {
                IdPertanyaan = recent[0].ToString(),
                IsiPertanyaan = recent[1].ToString(),
                PilihanJawaban1 = recent[2].ToString(),
                PilihanJawaban2 = recent[3].ToString(),
                PilihanJawaban3 = recent[4].ToString(),
                PilihanJawaban4 = recent[5].ToString(),
                BobotPillihanJawaban1 = Convert.ToInt32(recent[6].ToString()),
                BobotPillihanJawaban2 = Convert.ToInt32(recent[7].ToString()),
                BobotPillihanJawaban3 = Convert.ToInt32(recent[8].ToString()),
                BobotPillihanJawaban4 = Convert.ToInt32(recent[9].ToString())
            };
        }
        #endregion

        #region Events
        protected void gvPertanyaan_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("detailRecord"))
            {
                int index = Convert.ToInt32(e.CommandArgument);
                this.DetailCommand(index);
            }
            else if (e.CommandName.Equals("updateRecord"))
            {
                int index = Convert.ToInt32(e.CommandArgument);
                this.EditCommand(index);
            }
            else if (e.CommandName.Equals("deleteRecord"))
            {
                int index = Convert.ToInt32(e.CommandArgument);
                this.DeleteCommand(index);
            }
        }

        protected void gvPertanyaan_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvPertanyaan.PageIndex = e.NewPageIndex;
            this.InitTabelPertanyaan();

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "PagingScript", SiakadScript.PagerStyle(), false);
        }

        protected void btAdd_Click(object sender, EventArgs e)
        {
            this.txtAddPertanyaan.Text = string.Empty;
            this.txtAddPilihanJawaban1.Text = string.Empty;
            this.txtAddPilihanJawaban2.Text = string.Empty;
            this.txtAddPilihanJawaban3.Text = string.Empty;
            this.txtAddPilihanJawaban4.Text = string.Empty;
            //this.txtAddBobotPillihanJawaban1.Text = string.Empty;
            //this.txtAddBobotPillihanJawaban2.Text = string.Empty;
            //this.txtAddBobotPillihanJawaban3.Text = string.Empty;
            //this.txtAddBobotPillihanJawaban4.Text = string.Empty;

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowAddModalScript", PertanyaanScript.ShowAdd(), false);
        }

        protected void btDeletePertanyaan_Click(object sender, EventArgs e)
        {
            bool isSuccess = PertanyaanController.Hapus(hfDeleteID.Value);
            if (isSuccess)
            {
                this.InitTabelPertanyaan();

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "HideDeleteModalScript", PertanyaanScript.SuccessDelete(), false);
            }
        }

        protected void btEditPertanyaan_Click(object sender, EventArgs e)
        {
            var pertanyaan = new Pertanyaan
            {
                IdPertanyaan = this.hfUpdateID.Value,
                IsiPertanyaan = txtEditPertanyaan.Text,
                PilihanJawaban1 = txtEditPilihanJawaban1.Text,
                PilihanJawaban2 = txtEditPilihanJawaban2.Text,
                PilihanJawaban3 = txtEditPilihanJawaban3.Text,
                PilihanJawaban4 = txtEditPilihanJawaban4.Text,
                BobotPillihanJawaban1 = Convert.ToInt32(ddlEditBobotPillihanJawaban1.SelectedValue),
                BobotPillihanJawaban2 = Convert.ToInt32(ddlEditBobotPillihanJawaban2.SelectedValue),
                BobotPillihanJawaban3 = Convert.ToInt32(ddlEditBobotPillihanJawaban3.SelectedValue),
                BobotPillihanJawaban4 = Convert.ToInt32(ddlEditBobotPillihanJawaban4.SelectedValue),



            };

            bool isSuccess = PertanyaanController.Ubah(this.hfUpdateID.Value, pertanyaan);
            if (isSuccess)
            {
                this.InitTabelPertanyaan();

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "HideEditModalScript", PertanyaanScript.SuccessEdit(), false);
            }
        }

        protected void btAddPertanyaan_Click(object sender, EventArgs e)
        {
            var pertanyaan = new Pertanyaan
            {
                IdPertanyaan = PertanyaanController.GetID(),
                IsiPertanyaan = txtAddPertanyaan.Text,
                PilihanJawaban1 = txtAddPilihanJawaban1.Text,
                PilihanJawaban2 = txtAddPilihanJawaban2.Text,
                PilihanJawaban3 = txtAddPilihanJawaban3.Text,
                PilihanJawaban4 = txtAddPilihanJawaban4.Text,
                BobotPillihanJawaban1 = Convert.ToInt32(ddlAddBobotPillihanJawaban1.SelectedValue),
                BobotPillihanJawaban2 = Convert.ToInt32(ddlAddBobotPillihanJawaban2.SelectedValue),
                BobotPillihanJawaban3 = Convert.ToInt32(ddlAddBobotPillihanJawaban3.SelectedValue),
                BobotPillihanJawaban4 = Convert.ToInt32(ddlAddBobotPillihanJawaban4.SelectedValue),
            };

            bool isSuccess = PertanyaanController.Tambah(pertanyaan);
            if (isSuccess)
            {
                this.InitTabelPertanyaan();

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "HideAddModalScript", PertanyaanScript.SuccessAdd(), false);
            }
        }
        #endregion
    }
}